<?php
namespace app\techn_api\controller\v1;
use think\Db;
use app\service\MoneyLogService;
use think\Request;

class Money extends Base
{
	public function _initialize()
	{
		parent::_initialize();
		$this->IsLogin(); //验证登录
		$this->IsTechn(); //验证技师
	}

	/**
	 * 技师账户信息
	 */
	public function index(Request $request)
	{
        $type = $request->param();
        if (!empty($type['type'])){

        if ($type['type'] == 1){
            if (date('d',time())<16){
                $min = strtotime(date('Y-m-1',time()));
                $max = time();
                $time_str = date('Y.m.1',time()).'-'.date('Y.m',time()).'15';
            }else{
                $min = strtotime(date('Y-m-15',time()));
                $max = time();
                $time_str = date('Y.m.',time()).'15-'.date('Y.m.d',time());

            }
        }
        if ($type['type'] == 2){
            if (date('d',time())<16){
                $min = strtotime(date('Y-m-1',(time()-1296000)));
                $max = strtotime(date('Y-m-1',time()));
                $time_str = date('Y.m.1',(time()-1296000)).'-'.date('y.m.1',time());

            }else{
                $min = strtotime(date('Y-m-1',time()));
                $max = strtotime(date('Y-m-15',time()));
                $time_str = date('Y.m.1',time()).'-'.date('Y.m.15',time());

            }
        }

        if ($type['type'] == 3){
            $min = strtotime(date('Y',time()).'-'.((date('m',time()))-1).'-1');
            $max = strtotime(date('Y-m-1',time()));
            $time_str = date('Y',time()).'.'.((date('m',time()))-1).'.1-'.date('Y.m.1',time());

        }

        if ($type['type'] == 4){
            $min = strtotime(date('Y',time()).'-'.((date('m',time()))-2).'-1');
            $max = time();
            $time_str = date('Y',time()).'.'.((date('m',time()))-2).'.1-'.date('y.m.d',time());

        }

        }else{
            $min = strtotime('2023-01-01');
            $max = time();
            $time_str = '';
        }

		$techn = $this->techn;
		//总收入
		$techn['total_income'] = Db::name('money_log')->where('identity_type = "techn" and identity_id = '.$techn['id'].' and business_type = 5 and money_type = 0 and operation_type = 1')->sum('operation_money');
		//总车费
		$techn['total_travel_price'] = Db::name('money_log')->where('identity_type = "techn" and identity_id = '.$techn['id'].' and business_type = 4 and money_type = 0 and operation_type = 1')->sum('operation_money');
		//未入账
		$techn['total_no_money'] = Db::name('balance')->where('identity_type = "techn" and identity_id = '.$techn['id'].' and status = 0')->sum('no_money');

        $techn['total_allmoney'] = Db::name('money_log')->where('add_time','between',$min.','.$max)->where('identity_type = "techn" and identity_id = '.$techn['id'].' and business_type = 5 and money_type = 0 and operation_type = 1')->sum('operation_money');

        $techn['total_carmoney'] = Db::name('money_log')->where('add_time','between',$min.','.$max)->where('identity_type = "techn" and identity_id = '.$techn['id'].' and business_type = 4 and money_type = 0 and operation_type = 1')->sum('operation_money');
        $techn['total_servicemoney'] = Db::name('money_log')->where('add_time','between',$min.','.$max)->where('identity_type = "techn" and identity_id = '.$techn['id'].' and business_type = 5 and money_type = 0 and operation_type = 1')->sum('operation_money');
        $techn['time_str'] = $time_str;

		echo json_encode([
			'code' => 0,
			'msg' => '请求成功',
			'data' => $techn
		]);
	}

	/**
	 * 获取技师余额日志列表
	 */
	public function log_lists()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		$params = $this->data_post;
		$params['techn'] = $this->techn;
		//dump($params);
		//表单验证
		$validate = $this->validate(
			$params,
			[
				'page' => 'require'
			],
			[
				'page.require' => '分页有误'
			]
		);
		if(true !== $validate)
        {
            echo json_encode(['code' => -1, 'msg' => $validate]);
            exit();
        }
        $map = [
        	'identity_type' => 'techn',
        	'identity_id' => $params['techn']['id'],
        	'money_type' => 0
        ];

        if (!empty($params['type'])){
            if ($params['type'] == 1){
                if (date('d',time())<16){
                    $min = strtotime(date('Y-m-1',time()));
                    $max = time();
                }else{
                    $min = strtotime(date('Y-m-15',time()));
                    $max = time();

                }
            }
            if ($params['type'] == 2){
                if (date('d',time())<16){
                    $min = strtotime(date('Y-m-1',(time()-1296000)));
                    $max = strtotime(date('Y-m-1',time()));

                }else{
                    $min = strtotime(date('Y-m-1',time()));
                    $max = strtotime(date('Y-m-15',time()));

                }
            }

            if ($params['type'] == 3){
                $min = strtotime(date('Y',time()).'-'.((date('m',time()))-1).'-1');
                $max = strtotime(date('Y-m-1',time()));

            }

            if ($params['type'] == 4){
                $min = strtotime(date('Y',time()).'-'.((date('m',time()))-2).'-1');
                $max = time();

            }
            $maps['o.add_time'] = ['between',$min.','.$max];
        }
        $maps['o.techn_id'] = $params['techn']['id'];
//        $maps['o.status'] = ['in','6,16'];



        $list_data = Db::name('order')
            ->alias('o')
            ->where($maps)
            ->order('o.id desc')
            ->limit(($params['page']-1)*10,10)
            ->select();
//        dump($maps);die;
        if (!empty($list_data)){
            foreach ($list_data as $key => $value){
                $list_data[$key]['techn_money'] = Db::name('balance')->where('order_id',$value['id'])->where('identity_type','techn')->sum('yes_money');
                $list_data[$key]['add_time_time'] = date('Y-m-d H:i',$value['add_time']);
            }
        }
        $tmp = $list_data;
//        $list_data = Db::name('money_log')->where($map)->order('id desc')->limit(($params['page']-1)*10,10)->select();
        if(!empty($list_data))
        {
        	//业务类型
//        	$business_type_array = MoneyLogService::$business_type_array;
//        	//遍历处理
//        	foreach($list_data as $key => $value)
//        	{
//        		//业务类型
//        		$business_type_front_tip = ''; //业务类型前
//        		$business_type_behind_tip = ''; //业务类型后
//        		$business_type_name = !empty($business_type_array[$value['business_type']]['name']) ? $business_type_array[$value['business_type']]['name'] : '';
//        		switch ($value['business_type']) {
//        			//出行费用、服务费用
//        			case 4:
//        			case 5:
//        			    if($value['operation_type'] == 1)
//        			    {
//        			    	$business_type_front_tip = '收入-';
//        			    }
//        			break;
//        			//提现
//        			case 6:
//        			    if($value['operation_type'] == 1)
//        			    {
//        			    	$business_type_behind_tip = '失败退回'; //业务类型后
//        			    }
//        			break;
//        		}
//        		$list_data[$key]['business_type_name'] = $business_type_front_tip.$business_type_name.$business_type_behind_tip;
//        		//处理添加时间
//        		$list_data[$key]['add_time_time'] = !empty($value['add_time']) ? date('m月d日 H:i', $value['add_time']) : '';
//                $order_info = [];
//                $list_data[$key]['order_info'] = $order_info;
//
//                if ($value['from_order_id'] != 0){
//                    $order_info = Db::name('order')
//                        ->alias('a')
//                        ->join('order_detail d',' a.id = d.order_id')
//                        ->where('a.id',$value['from_order_id'])
//                        ->field('a.order_no,d.title,a.jztype,a.techn_id')
//                        ->find();
//                    $list_data[$key]['order_info'] = $order_info;
//                    $list_data[$key]['business_type_name'] = $business_type_front_tip."'".$order_info['title']."'-".$business_type_name.$business_type_behind_tip;
//
//                    if (!empty($order_info) && $order_info['jztype'] == 1 && $value['business_type'] == 5){
//                        $jz_info = Db::name('order_jzdetail')->where('order_id',$value['from_order_id'])->find();
//                        $techn_info = Db::name('techn_last')->where('techn_id',$order_info['techn_id'])->find();
//                        if ($techn_info['jz_bl'] > 0){
//                            $bl = $techn_info['jz_bl'];
//                        }else{
//                            $bl = $techn_info['divide_into'];
//                        }
//                        if ($jz_info['price'] * $bl/100 == $value['operation_money']){
//                            $list_data[$key]['business_type_name'] = $business_type_front_tip."'(加钟)".$order_info['title']."'-".$business_type_name.$business_type_behind_tip;
//
//                        }
//                    }
//                }
//        	}


        	$result['code'] = 0;
	        $result['msg'] = "请求成功";
	        $result['data'] = $list_data;
	        $result['data'] = $tmp;
        }else
        {
        	$result['code'] = -1;
	        $result['msg'] = "暂无数据";
        }
        echo json_encode($result);
	}

    public function detail(Request $request){
        $order_id = $request->param();
        if (!empty($order_id['order_id'])){
            $info = Db::name('order')->where('id',$order_id['order_id'])->find();
            if (!empty($info)&& $info['jztype'] == 1){
                $info['jiaz'] = Db::name('order_jz')->alias('j')->join('order_jzdetail d','j.id = d.jz_id')->where('j.order_id',$info['id'])->field('j.*,d.title,d.buy_number,d.price as dprice')->select();
            }

            $info['qudao'] = Db::name('balance')->where('order_id',$info['id'])->where('balance_type','in','4,5')->where('identity_type','user')->sum('yes_money');
            $info['techn_service'] = round(Db::name('balance')->where('order_id',$info['id'])->where('balance_type','in','1')->where('identity_type','techn')->sum('yes_money'),2);
            $info['techn_car'] = round(Db::name('balance')->where('order_id',$info['id'])->where('balance_type','in','2')->where('identity_type','techn')->sum('yes_money'),2);

//            $info['techn_service'] = round($info['techn_service'],2);
//            $info['techn_car'] = round($info['techn_car'],2);
            $info['increase_price'] = $info['preferential_price'];
            $info['service_time'] = date('Y-m-d H:i',$info['pre_start_time']).'-'.date('H:i',$info['pre_end_time']);
            $info['add_time_time'] = date('Y-m-d H:i',$info['add_time']);
            $info['techn_last'] = Db::name('techn_last')->where('techn_id',$info['techn_id'])->find();
            $info['qudao'] = round($info['qudao'] * $info['techn_last']['divide_into'] / 100,2);
            $info['detail'] = Db::name('order_detail')->where('order_id',$info['id'])->order('id desc')->find();

        }

        if (!empty($info)){
            echo json_encode(['code'=>0,'msg'=>'ok','data'=>$info]);
        }else{
            echo json_encode(['code'=>-1,'msg'=>'暂无数据']);

        }
    }
}